package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.business.vo.DrInfoImport;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 初始化项目设备列表专用Controller
 * 
 * @author yaozh
 * @date 2024-03-01
 */
@RestController
@RequestMapping("/system/drinfoinit")
public class DrInfoInitController extends BaseController
{
    @Autowired
    private IDrinfoService drinfoService;

    @Autowired
    private IRegGroupService regGroupService;
    @Autowired
    private IDrtypeinfoService drtypeinfoService;
    @Autowired
    private IAlarmtypeService alarmtypeService;
    @Autowired
    private IAppmanagerService appManagerService;
    @Resource
    private ISubService subService;

    /**
     * 查询初始化项目设备列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:list')")
    @GetMapping("/list")
    public TableDataInfo list(Drinfo drinfo,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<Drinfo> list = drinfoService.selectDrinfoList(drinfo,dbName);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:drinfoinit:list')")
    @GetMapping("/listByDrTypeId/{drtypeid}")
    public AjaxResult listByDrTypeId(@PathVariable("drtypeid") Long drtypeid,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        List<Drinfo> list = drinfoService.selectByDrTypeId(drtypeid,dbName);
        return success(list);
    }

    /**
     * 导出初始化项目设备列表列表
     */
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:export')")
    @Log(title = "初始化项目设备列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Drinfo drinfo,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        List<Drinfo> list = new ArrayList<>();
        if(appList != null && appList.size() > 0){
            list = drinfoService.selectDrinfoList(drinfo,dbName);
        }
        ExcelUtil<Drinfo> util = new ExcelUtil<Drinfo>(Drinfo.class);
        util.exportExcel(response, list, "初始化项目设备列表数据");
    }

    @Log(title = "初始化项目设备列表", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, @RequestParam String dbName) throws Exception
    {
        ExcelUtil<DrInfoImport> util = new ExcelUtil<DrInfoImport>(DrInfoImport.class);
        List<DrInfoImport> qsTagList = util.importExcel(file.getInputStream());
        String operaName = getUsername();
        String message = drinfoService.importDrInfoInit(qsTagList, updateSupport, operaName, dbName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Qstag> util = new ExcelUtil<Qstag>(Qstag.class);
        util.importTemplateExcel(response, "初始化项目设备信息的设备列表清单");
    }

    /**
     * 获取初始化项目设备列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:query')")
    @GetMapping(value = "/{drid}")
    public AjaxResult getInfo(@PathVariable("drid") Long drid,@RequestParam String dbName)
    {
        return success(drinfoService.selectDrinfoByDrid(drid,dbName));
    }

    /**
     * 新增初始化项目设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:add')")
    @Log(title = "初始化项目设备列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Drinfo drinfo,@RequestParam String dbName)
    {
        return toAjax(drinfoService.insertDrinfo(drinfo,dbName));
    }

    /**
     * 修改初始化项目设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:edit')")
    @Log(title = "初始化项目设备列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Drinfo drinfo,@RequestParam String dbName)
    {
        return toAjax(drinfoService.updateDrinfo(drinfo,dbName));
    }

    /**
     * 删除初始化项目设备列表
     */
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:remove')")
    @Log(title = "初始化项目设备列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{drids}")
    public AjaxResult remove(@PathVariable Long[] drids,@RequestParam String dbName)
    {
        return toAjax(drinfoService.deleteDrinfoByDrids(drids,dbName));
    }

    @PreAuthorize("@ss.hasPermi('system:drinfoinit:list')")
    @GetMapping("/listAllGroup")
    public AjaxResult listAllGroup(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(regGroupService.selectRegGroupForAll(dbName));
    }

    /**
     * 不分页，查询项目类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:list')")
    @GetMapping("/listAllDrTypeInfo")
    public AjaxResult listAllDrTypeInfo(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(drtypeinfoService.selectDrTypeForAll(dbName));
    }

    /**
     * 获取设备类型树列表
     */
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:list')")
    @GetMapping("/drTypeTree")
    public AjaxResult drTypeTree(Drtypeinfo drtypeinfo,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(drtypeinfoService.selectDrTypeTreeList(drtypeinfo,dbName));
    }

    /**
     * 不分页，查询驱动列表
     */
    @PreAuthorize("@ss.hasPermi('system:drinfoinit:list')")
    @GetMapping("/listAllAlarmType")
    public AjaxResult listAllAlarmType(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(alarmtypeService.selectAlarmtypeForAll(dbName));
    }

    @PreAuthorize("@ss.hasPermi('system:drinfoinit:list')")
    @GetMapping("/subAllList")
    public AjaxResult subAllList(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(subService.selectSubList(new Sub(),dbName));
    }

}
